package com.zld.smarket.controller;

import com.zld.smarket.pojo.*;
import com.zld.smarket.result.ExceptionMsg;
import com.zld.smarket.result.ResponseData;
import com.zld.smarket.service.OnshelvesService;
import com.zld.smarket.service.ShelvesService;
import com.zld.smarket.service.ShelvesonService;
import com.zld.smarket.service.StockoutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/shelveson")
public class ShelvesonController {
    @Autowired
    private ShelvesonService shelvesonService;
    @Autowired
    private StockoutService stockoutService;
    @Autowired
    private ShelvesService shelvesService;
    @Autowired
    private OnshelvesService onshelvesService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseData findList(Integer page, Integer limit) {
        return shelvesonService.findList(page, limit);
    }

    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public ResponseData search(String s,Integer page, Integer limit){
        return  shelvesonService.search(s,page,limit);
    }
    @RequestMapping(value = "/weekshelveson",method = RequestMethod.GET)
    public Long weekshelveson(){
        return  shelvesonService.weekshelveson();
    }


    @RequestMapping(value = "/setShelveson")
    public ResponseData setShelveson(Shelveson shelveson){
        Long stockoutId = shelveson.getId();
        shelveson.setId(null);
        Long i=shelveson.getShelvesonNo();
        Long j=shelveson.getShelvesNo();
        if(shelvesonService.selectbyno(i)>0){
            return new ResponseData(ExceptionMsg.ADDERROR);
        }else {
            Shelves shelves =shelvesService.select(j);
            if (shelves.getShelvesNum()+shelveson.getShelvesonNum()>shelves.getShelvesMaxnum() ) {
                return new ResponseData(ExceptionMsg.OUTOFNUM2);
            } else {
                shelveson.setShelvesonDate(new Date());
                List<Onshelves> onshelves = onshelvesService.selectGoods(shelveson.getGoodsNo(),shelveson.getShelvesNo(),shelveson.getSupplierId());
                Integer integer = shelvesonService.setShelveson(shelveson);
                Integer integer1 = stockoutService.reduceSurplus(stockoutId, shelveson.getShelvesonNum());
                Integer integer2 = shelvesService.addShelvesNum(shelves.getShelvesNo(),shelveson.getShelvesonNum());
                if(onshelves.size()==1){
                Integer integer3 = onshelvesService.addnum(onshelves.get(0).getId(),shelveson.getShelvesonNum());
                }else{
                    Onshelves onshelve = new Onshelves();
                    onshelve.setId(null);
                    onshelve.setGoodsNo(shelveson.getGoodsNo());
                    onshelve.setShelvesNo(shelveson.getShelvesNo());
                    onshelve.setOnshelvesNum(shelveson.getShelvesonNum());
                    onshelve.setSupplierId(shelveson.getSupplierId());
                    onshelve.setOnshelvesNumofwarn(30L);
                    onshelve.setOnshelvesState(1L);
                    Integer integer3 = onshelvesService.addOnshelves(onshelve);

                }

                return new ResponseData(ExceptionMsg.SUCCESS);
            }
        }

        }


}
